home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / mailutil.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  6KB  |  248 lines

  1. /****************************************************************************
  2. *                                                                           *
  3. *    mailutil.c - A few functions from bmutil.c and mailbox.c                 *
  4. *                                                                           *
  5. *  29.08.94 DFN -    Initial version                                                         *
  6. *                                                                           *
  7. ****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. #include "global.h"
  14. #include "ftpserv.h"
  15. #include "smtp.h"
  16. #include "proc.h"
  17. #include "usock.h"
  18. #include "socket.h"
  19. #include "telnet.h"
  20. #include "timer.h"
  21. #include "session.h"
  22. #include "files.h"
  23. #include "bm.h"
  24. #include "mailbox.h"
  25.  
  26. char *rewrite_address(char *addr);
  27.  
  28. static void string_tidy(char *str);
  29.    
  30. /*---------------------------------------------------------------------------
  31.     Hdrs - Array of valid mail headers, used by the htype function.
  32. ---------------------------------------------------------------------------*/
  33.  
  34. char *Hdrs[] = {
  35.     "Approved: ",
  36.     "From: ",
  37.     "To: ",
  38.     "Date: ",
  39.     "Message-Id: ",
  40.     "Subject: ",
  41.     "Received: ",
  42.     "Sender: ",
  43.     "Reply-To: ",
  44.     "Status: ",
  45.     "X-BBS-Msg-Type: ",
  46.     "X-Forwarded-To: ",
  47.     "Cc: ",
  48.     "Return-Receipt-To: ",
  49.     "Apparently-To: ",
  50.     "Errors-To: ",
  51.     "Organization: ",
  52.     NULLCHAR
  53. };
  54.  
  55.  
  56. /*---------------------------------------------------------------------------
  57.     htype - Return the header type
  58. ---------------------------------------------------------------------------*/
  59.     
  60. int htype(char *s)
  61. {
  62.     int    i;
  63.     char  *p;
  64.  
  65.     p = s;
  66.  
  67.     /* check to see if there is a ':' before and white space                     */
  68.  
  69.     while (*p != '\0' && *p != ' ' && *p != ':')
  70.         p++;
  71.         
  72.     if (*p != ':')
  73.         return NOHEADER;
  74.  
  75.     for (i = 0; Hdrs[i] != NULLCHAR; i++) {
  76.         if (strnicmp(Hdrs[i], s, strlen(Hdrs[i])) == 0)
  77.             return i;
  78.     }
  79.     return UNKNOWN;
  80. }
  81.  
  82. /*---------------------------------------------------------------------------
  83.     getaddress - Parse a string to extract an address of the form 'user@host'
  84.                  Vaild input formats:-  Text: <user@host>
  85.                                         Text: user@host (Text)
  86. ---------------------------------------------------------------------------*/
  87.  
  88. char *getaddress(char *string, int cont)
  89. {
  90.     int par = 0;
  91.     char *cp, *ap = NULLCHAR;
  92.  
  93.     if ((cp = getname(string)) != NULLCHAR)     /* Look for <> style address     */
  94.          return cp;
  95.          
  96.     cp = string;
  97.     
  98.     if (!cont)
  99.         if ((cp = strchr(string, ':')) == NULLCHAR)    /* Skip the token         */
  100.             return NULLCHAR;
  101.         else
  102.             ++cp;
  103.             
  104.     for ( ; *cp != '\0'; ++cp) {
  105.         if (par && *cp == ')') {
  106.             --par;
  107.             continue;
  108.        }
  109.        
  110.        if (*cp == '(')                                /* Ignore text within ()         */
  111.             ++par;
  112.             if (par)
  113.                   continue;
  114.              if (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == ',') {
  115.                   if (ap != NULLCHAR)
  116.                    break;
  117.                   continue;
  118.               }
  119.               
  120.          if (ap == NULLCHAR)
  121.               ap = cp;
  122.     }
  123.     
  124.     *cp = '\0';
  125.     return ap;
  126. }
  127.  
  128.  
  129. /*---------------------------------------------------------------------------
  130.     rewrite_address - Read the rewrite file for lines where the first word is
  131.                            a regular expression and the second word are rewriting
  132.                      rules.  The special character '$' followed by a digit
  133.                      denotes the string that matched a '*' character. The '*'
  134.                      characters are numbered from 1 to 9.  Example: the line
  135.                      "*@*.* $2@$1.ampr.org" would rewrite the address
  136.                      "foo@bar.xxx" to "bar@foo.ampr.org".
  137.                      $H is replaced by our hostname, and $$ is an escaped $
  138.                      character.  If the third word on the line has an 'r'
  139.                      character in it, the function will recurse with the new
  140.                      address.
  141. ---------------------------------------------------------------------------*/
  142.  
  143. char *rewrite_address(char *addr)
  144. {
  145.     int cnt;
  146.     char *argv[10], *cp, *cp2, *retstr;
  147.     char buf[MBXLINE];  
  148.     FILE *fp;
  149.     
  150.     if ((fp = fopen(Rewritefile, READ_TEXT)) == NULLFILE)
  151.         return NULLCHAR;
  152.         
  153.     memset((char *)argv, 0, 10*sizeof(char *));
  154.     
  155.     while (fgets(buf, MBXLINE, fp) != NULLCHAR) {
  156.         if (*buf == '#')                                /* skip commented lines         */
  157.             continue;
  158.  
  159.         string_tidy(buf);                               /* clean up the buffer             */
  160.  
  161.         if ((cp = strchr(buf, ' ')) == NULLCHAR) /* get the first word         */
  162.             if ((cp = strchr(buf, '\t')) == NULLCHAR)
  163.                 continue;
  164.  
  165.         *cp = '\0';
  166.         
  167.         if ((cp2 = strchr(buf, '\t')) != NULLCHAR) {
  168.             *cp = ' ';
  169.             cp  = cp2;
  170.             *cp = '\0';
  171.         }
  172.         
  173.         if (!wildmat(addr, buf, argv))
  174.             continue;                                    /* no match                         */
  175.         rip(++cp);
  176.         cp2 = retstr = (char *)callocw(1, MBXLINE);
  177.  
  178.         while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  179.             if (*cp == '$') {
  180.                 if (isdigit(*(++cp)))
  181.                     if (argv[*cp - '0'-1] != '\0')
  182.                         strcat(cp2, argv[*cp - '0'-1]);
  183.  
  184.                 if (*cp == 'h' || *cp == 'H')     /* Our hostname                     */
  185.                     strcat(cp2, Hostname);
  186.  
  187.                 if (*cp == '$')                        /* Escaped $ character             */
  188.                     strcat(cp2, "$");
  189.                 cp2 = retstr + strlen(retstr);
  190.                 cp++;
  191.             }
  192.             else
  193.                 *cp2++ = *cp++;
  194.                 
  195.         for (cnt=0; argv[cnt] != NULLCHAR; ++cnt)
  196.             free(argv[cnt]);
  197.             
  198.         fclose(fp);
  199.         
  200.         /*
  201.             If there remains an 'r' character on the line, repeat everything
  202.             by recursing.
  203.         */
  204.         
  205.         if (strchr(cp, 'r') != NULLCHAR || strchr(cp, 'R') != NULLCHAR) {
  206.             if ((cp2 = rewrite_address(retstr)) != NULLCHAR) {
  207.                 free(retstr);
  208.                 return cp2;
  209.             }
  210.         }
  211.         return retstr;
  212.     }
  213.     
  214.     fclose(fp);
  215.     return NULLCHAR;
  216. }
  217.  
  218.  
  219. /*---------------------------------------------------------------------------
  220.     stribg_tidy - Reduce runs of whitespace to 1 space
  221. ---------------------------------------------------------------------------*/
  222.     
  223. static void string_tidy(char *str)   
  224. {
  225.     int c;
  226.     char *ip=str;
  227.     char *op=str;
  228.  
  229.     while (c = *ip++) {
  230.         if (c != ' ' && c != '\t') {
  231.           *op++ = c;
  232.           continue;
  233.         }
  234.  
  235.         *op++ = ' ';
  236.  
  237.         while (*ip && (*ip == ' ' || *ip =='\t'))
  238.             ip++;
  239.    }
  240.    
  241.     *op='\0';  
  242. }
  243.  
  244. /****************************************************************************
  245. *    T H E   E N D                                                                                 *
  246. ****************************************************************************/ 
  247.     
  248.